﻿class SearchController < BaseController
  
  def index  
    @query = q = params[:q] || {}
    @news = News.check(2).page(params[:page]).per(25).order("pubtime desc")
    category_id = q[:category_id] if q[:category_id].present?
    children_id = q[:children_id] if q[:children_id].present?
    
    if(category_id=="21")
      @news = News.page(params[:page]).per(25).order("pubtime desc")
    else
      @news = News.check(2).page(params[:page]).per(25).order("pubtime desc")
      end
        
      
    @news = @news.where("category_id=?",category_id) if category_id.present? && category_id!='请选择'
    @news = @news.where("pubtime>=?", q[:begin_time].to_date.beginning_of_day.strftime("%Y-%m-%d %H:%M:%S") ) if q[:begin_time].present?
    @news = @news.where("pubtime<=?", q[:end_time].to_date.at_end_of_day.strftime("%Y-%m-%d %H:%M:%S") ) if q[:end_time].present?
    @news = @news.where(find_keywords(q) || {})
    @news = @news.where(*find_keywords(q)) if find_keywords(q).present?    
    @news = @news.where(:writer => q[:writer]) if q[:writer].present?
    @news = @news.where(:contribution_units_id => q[:department_id]) if q[:department_id].present? && q[:department_id].to_i > 0      
    @news = @news.where(find_news_type(q)) if find_news_type(q).present?

 if(category_id=="21")
     @news1 = News
    else
      @news1 = News.check(2)
      end
      
    
    @news1 = @news1.where("category_id=?",category_id) if category_id.present? && category_id!='请选择'
    @news1 = @news1.where("pubtime>=?", q[:begin_time].to_date.beginning_of_day.strftime("%Y-%m-%d %H:%M:%S")) if q[:begin_time].present?
    @news1 = @news1.where("pubtime<=?", q[:end_time].to_date.at_end_of_day.strftime("%Y-%m-%d %H:%M:%S")) if q[:end_time].present?
    @news1 = @news1.where(find_keywords(q) || {})
    @news1 = @news1.where(*find_keywords(q)) if find_keywords(q).present?
    @news1 = @news1.where(:writer => q[:writer]) if q[:writer].present?
    @news1 = @news1.where(:contribution_units_id => q[:department_id]) if q[:department_id].present? && q[:department_id].to_i > 0
    @news1 = @news1.where(find_news_type(q)) if find_news_type(q).present?
    @news_count = 0
    @news_count = @news1.size 
    puts "sql: #{@news.to_sql}"
  end

  def advanced 
    @categories = Category.root - Category.where(:cataname => ['高教纵览','党群连线'])
    @departments = Department.all
  end

  private

    def find_category(q)
      if q[:category_id].present? &&  q[:category_id].to_i > 0         
        {:category_id => q[:category_id]}                  
      end
    end

    def find_news_type(q)
      if q[:is_important] == "on" && q[:is_not_important_news].blank?
        {is_important_news: 1}
      elsif q[:is_not_important_news] == "on" && q[:is_important].blank?
        {is_important_news: 0}
      end
    end

    def find_keywords(q)      
      if q[:keywords].present?
        unless q[:check] == "on"
          ["title like ?", "%#{q[:keywords]}%"]
        else
          ["title like ? or body like ? or keywords like ?", "%#{q[:keywords]}%", "%#{q[:keywords]}%", "%#{q[:keywords]}%"]
        end
      end
    end
end